/** 
 * @jsName: ElevatorInfoManaged.js 
 * @author Sean Cui
 * @date 2017-04-16 下午3:46:00 
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @version V1.0.0.0
 * @comment 该文件通过代码生成器生成，如果修改次文件后，重新生成时将覆盖修改内容，请修改生成模板或做好备份
 */

var ElevatorInfoManaged = function() {
	return {
		datatable : null,
		init : function() {
			this.initSearch();
			this.initListener();
			this.initKindEditor();
		},
		initSearch : function() {
			if (!jQuery().dataTable) {
				return;
			}
			var listTable = $("#ElevatorInfoListTable").dataTable({
				"sAjaxSource" : ctx + "/elevatorInfo/searchForTenance?now=" + new Date().getTime(),
				/* 使用post方式 */
				"fnServerData" : function(sSource, aoData, fnCallback) {
					var postData = {
						page : 1,
						start : 0,
						rows : 10,
						params : {}
					};
					
					if($('#archiveNo').val()){
						postData.params.archiveNo = ($('#archiveNo').val()).replace(/\s/g,"%");
					}
					if($('#registerCode').val()){
						postData.params.registerCode = ($('#registerCode').val()).replace(/\s/g,"%");
					}
					if($('#placeType').val()){
						postData.params.placeType = ($('#placeType').val()).replace(/\s/g,"%");
					}
					if($('#deviceState').val()){
						postData.params.deviceState = ($('#deviceState').val()).replace(/\s/g,"%");
					}

					// 参数
					for (var i = 0, size = aoData.length; i < size; i++) {
						if (aoData[i].name == 'iDisplayLength') {
							postData.rows = aoData[i].value;
						} else if (aoData[i].name == 'iDisplayStart') {
							postData.start = aoData[i].value;
						} else if (aoData[i].name == 'sEcho') {
							postData.page = aoData[i].value;
						}
					}

					$.ajax({
						"dataType" : 'json',
						"type" : "POST",
						"cache": false,
						"url" : sSource,
						"data" : postData,
						"success" : function(data, textStatus, jqXHR){
							fnCallback(data.data, textStatus, jqXHR);
						}
					});
				},
				"bAutoWidth":false,
				"aoColumnDefs" : [
						{
									"data" : "index",
									"aTargets" : [ 0 ]
								},
								{
									"aTargets" : [ 1 ],
									"mRender" : function(data, type, full) {
										var htmls =  '';
										htmls +=  full.registerCode;
										return htmls;
									}
								},
								{
									"aTargets" : [ 2 ],
									"mRender" : function(data, type, full) {
										var htmls =  '';
										htmls +=  full.deviceName;
										return htmls;
									}
								},
								{
									"aTargets" : [ 3 ],
									"mRender" : function(data, type, full) {
										var htmls =  '';
										htmls +=  full.placeType;
										return htmls;
									}
								},
								{
									"aTargets" : [ 4 ],
									"mRender" : function(data, type, full) {
										var htmls =  '';
										htmls +=  full.tenanceUnitname;
										return htmls;
									}
								},
								{
									"aTargets" : [ 5 ],
									"mRender" : function(data, type, full) {
										var htmls =  '';
										htmls += full.tenanceTel;
										return htmls;
									}
								},
								{
									"aTargets" : [ 6 ],
									"mRender" : function(data, type, full) {
										var htmls = '';
										htmls +=  full.useunitName;
										return htmls;
									}
								},
								{
									"aTargets" : [ 7 ],
									"mRender" : function(data, type, full) {
										var htmls =  '';
										htmls += full.deviceUseposition;
										return htmls;
									}
								},
								{
									"aTargets" : [ 8 ],
									"mRender" : function(data, type, full) {
										var htmls = '';
										htmls +=  full.deviceState;
										return htmls;
									}
								},
								{
									"aTargets" : [ 9 ],
									"mRender" : function(data, type, full) {
										var htmls =  '';
										htmls += "已投保";
										return htmls;
									}
								},
								{
									"aTargets" : [ 10 ],
									"mRender" : function(data, type, full) {
										var btns =  '';
										//btns +=  Permission.createPermElement('elevatorInfo:list:modify',' <button title="'+messages.public_button_edit+'" class="btn btn-sm btn-primary" name="edit" target_id="'+full.id+'"><i class="fa fa-pencil-square-o"></i></button>');
										btns +=  Permission.createPermElement('elevatorInfo:list:del',' <button title="'+messages.public_button_del+'" href="javascript:;" class="btn btn-sm  btn-danger" name="del" target_id="'+full.id+'"><i class="fa fa-times"></i></button>');
										return btns;
									}
								}
				]
			});
			this.datatable = listTable;
		},reSearch:function(){
        	this.datatable.fnDraw();
        },removeClass:function(){//去除Form表单的验证样式
        	$('.form-group','#actForm').removeClass('has-success')
            .removeClass('has-error');
    		 $('span').remove('.help-block');
        },initListener : function() {
			/*
			 * 查询
			 */
			$("#searchBtn").click(function() {
				ElevatorInfoManaged.reSearch();
				return false;
			});

			/*
			 * 清除
			 */
			$("#clearBtn").click(function() {
				$("#searchForm")[0].reset();
				ElevatorInfoManaged.reSearch();
			});

			/*
			 * 添加
			 */
			$("#addBtn").click(function() {
				ElevatorInfoManaged.removeClass();
				$("div[name=editDlg] h4.modal-title").html("新增");
				$("#actForm")[0].reset();
				$("#actForm").find("input[name='id']").val("");
				$("#editDlg").modal("show");
			});
			
			$("#actFormBtn").click(function(){
				var form1 = $('#actForm');
				form1.validate({
					rules : {
						archiveNo:{
							maxlength : 25,
							required : true
						},
						registerCode:{
							maxlength : 25,
							required : true
						},
						deviceName:{
							maxlength : 25,
							required : true
						},
						deviceType:{
							maxlength : 25,
							required : true
						},
						deviceUseposition:{
							maxlength : 25,
							required : true
						},
						placeType:{
							maxlength : 25,
							required : true
						},
						placeArea:{
							maxlength : 25,
							required : true
						},
						placeAddress:{
							maxlength : 25,
							required : true
						},
						deviceState:{
							maxlength : 25,
							required : true
						},
						superviceState:{
							maxlength : 25,
							required : true
						},
						importTime:{
							maxlength : 25,
							required : true
						},
						areaId:{
							maxlength : 25,
							required : true
						},
						detailId:{
							maxlength : 25,
							required : true
						},
						classifyId:{
							maxlength : 25,
							required : true
						},
						useplaceId:{
							maxlength : 25,
							required : true
						},
						limityearId:{
							maxlength : 25,
							required : true
						}
					}
				});
				if(form1.valid()){
					var postData = $("form[name='actForm']").serializeJson();
					$.ajax({
						"dataType" : 'json',
						"cache": false,
						"type" : "POST",
						"data" : postData,
						"url" : ctx + "/elevatorInfo/edit?now="+new Date().getTime(),
						"success" : function(data) {
							if(data.type=='success'){
								$("#editDlg").modal("hide");
								BootstrapDialog.show({
									title : messages.public_cue_cue,
									message : messages.public_cue_saveOk,
									buttons : [ {
										label : messages.public_cue_sure,
										cssClass : 'btn btn-sm btn-primary',
										action : function(dialogItself) {
											ElevatorInfoManaged.reSearch();
											dialogItself.close();
										}
									} ]
								});
								
							}else{
								BootstrapDialog.show({
									type : BootstrapDialog.TYPE_WARNING,
									title : messages.public_cue_warning,
									message : data.message,
									buttons : [ {
										label : messages.public_cue_sure,
										cssClass : 'btn btn-sm btn-primary',
										action : function(dialogItself) {
											ElevatorInfoManaged.reSearch();
											dialogItself.close();
										}
									} ]
								});
							}
						}
					});
				}
			});
			
			/*
			 * 编辑
			 */
			$('#ElevatorInfoListTable tbody')
				.on('click', 'button[name="edit"]',function() {
					var id = $(this).attr("target_id");
					var form=$("form[name='actForm']");
					ElevatorInfoManaged.removeClass();
					$.ajax({
						"dataType" : 'json',
						"cache" : false,
						"type" : "POST",
						"data" : {},
						"url" : ctx + '/elevatorInfo/get/'+id+"?now="+new Date().getTime(),
						"success" : function(data, textStatus, jqXHR){
							if (data.type == 'success') {
								$(form).binddata(data.data);
								$("div[name=editDlg] h4.modal-title").html("编辑角色");
								$("#editDlg").modal("show");
							} else {
								BootstrapDialog.show({
									title : messages.public_cue_message,
									message : data.message,
									buttons : [ {
										label : messages.public_cue_sure,
										action : function(dialog) {
											dialog.close();
										}
									} ]
								});
							}
						}
					});
				});
			$('#ElevatorInfoListTable tbody')
				.on('click', 'button[name="del"]',function() {
					var id = $(this).attr("target_id");
					ElevatorInfoManaged.del(id);
				});
		},
		/**
		 * 删除
		 */
		del : function(id) {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : messages.public_cue_warning,
				message : messages.public_cue_delSure,
				buttons : [ {
					label : messages.public_cue_close,
					cssClass: 'btn btn-sm btn-default',
					action : function(dialogItself) {
						dialogItself.close();
					}
				}, {
					label : messages.public_cue_sure,
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						ElevatorInfoManaged.sureDel(id);
						dialogItself.close();
					}
				} ]
			});
		},
		sureDel : function(id){
			$.ajax({
				"dataType" : 'json',
				"cache": false,
				"type" : "POST",
				"data" : {},
				"url" : ctx + '/elevatorInfo/delete/'+id+"?now="+new Date().getTime(),
				"success" : function(data, textStatus, jqXHR){
					if (data.type == 'success') {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : messages.public_cue_delSuccess,
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									ElevatorInfoManaged.reSearch();
									dialog.close();
								}
							} ]
						});
					} else {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : data.message,
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									dialog.close();
								}
							} ]
						});
					}
				}
			});
		},
		initKindEditor:function(){
			
			KindEditor.ready(function(K) {
				//图片上传组件
				var editor = K.editor({
					uploadJson : ctx + '/kindeditor/uploadFileJson',
					fileManagerJson : ctx + '/kindeditor/fileManagerJson',
					allowFileManager : true
				});
				
				K('#importBtn').click(
					function() {
					editor.loadPlugin('insertfile', function() {
						editor.plugin.fileDialog({
							showRemote : true,
							clickFn : function(url, title, width,
									height, border, align) {
								if(null!=url&&url!=""){
									urlArr = url.split("/");
									var uData = {
											params : {tempPath:("/"+urlArr[urlArr.length-2]+"/"+urlArr[urlArr.length-1])}
										};
									$.ajax({
										"dataType" : 'json',
										"cache": false,
										"type" : "POST",
										"url" : ctx+"/elevatorInfo/import/excel?now="+new Date().getTime(),
										"data" : uData,
										"success" : function(data, textStatus, jqXHR){
											BoxManaged.reSearch();
										}
									});
								}
								editor.hideDialog();
							}
						});
					});
				});
			});
		}
	};

}();

/*
 * Powered By [Code-Generator]
 * Web Site: http://www.hwelltech.com
 */

